// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.dbmanager.ui;

import java.util.List;

import org.dengues.commons.sql.data.DataSetRow;
import org.dengues.commons.ui.swt.table.TableViewerWithMultipage;
import org.dengues.core.sql.IDataEditor;
import org.dengues.warehouse.dbmanager.ui.action.AbstractToolAction;
import org.dengues.warehouse.dbmanager.ui.action.RunSqlAction;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-6 qiang.zhang $
 * 
 */
public abstract class AbstractEditorComposite extends Composite implements IDataEditor {

    public static int OBJECT = 0;

    public static int DATA = 1;

    public static int SQL = 2;

    protected ToolBar coolBar;

    protected ToolBarManager defaultToolBarMgr;

    // protected AbstractToolAction cancelAction;

    // protected AbstractToolAction addRowAction;

    // protected AbstractToolAction removeRowAction;

    protected AbstractToolAction sqlRunAction;

    // protected AbstractToolAction cleanAllAction;

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractEditorComposite constructor comment.
     */
    public AbstractEditorComposite(Composite parent, int style) {
        super(parent, style);
        setBackground(parent.getBackground());
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginBottom = 0;
        gridLayout.marginWidth = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginHeight = 0;

        gridLayout.horizontalSpacing = 0;
        gridLayout.verticalSpacing = 0;

        setLayout(gridLayout);
        setLayoutData(new GridData(GridData.FILL_BOTH));
        initComponents();
        addListeners();
    }

    protected abstract void initComponents();

    protected abstract void addListeners();

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createToolBar".
     * 
     * @param id
     * @param composite
     */
    protected void createToolBar(int id, Composite composite) {

        coolBar = new ToolBar(composite, SWT.RIGHT | SWT.FLAT);

        GridData gid = new GridData(GridData.FILL_HORIZONTAL);
        coolBar.setLayoutData(gid);

        GridLayout layout = new GridLayout();
        layout.marginLeft = 0;
        layout.marginTop = 0;
        layout.marginBottom = 0;
        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        layout.marginRight = 0;
        coolBar.setLayout(layout);

        defaultToolBarMgr = new ToolBarManager(coolBar);

        initDefalutTooBar(id);
        // coolBarMgr.add(new ToolBarContributionItem(defaultToolBarMgr));
        defaultToolBarMgr.update(true);

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "initDefalutTooBar".
     * 
     * @param id
     */
    private void initDefalutTooBar(int id) {
        defaultToolBarMgr.removeAll();
        if (id == SQL) {
            sqlRunAction = new RunSqlAction(this, id);
            defaultToolBarMgr.add(sqlRunAction);

            // cancelAction = new CancelAction(this);
            // cancelAction.setEnabled(false);
            // defaultToolBarMgr.add(cancelAction);
        }
        // cleanAllAction = new CleanAllAction(this, id);
        // defaultToolBarMgr.add(cleanAllAction);
        if (id == DATA) {
            // defaultToolBarMgr.add(new Separator());
            // addRowAction = new AddRowAction(this);
            // addRowAction.setEnabled(false);
            // defaultToolBarMgr.add(addRowAction);
            //
            // removeRowAction = new RemoveRowAction(this);
            // removeRowAction.setEnabled(false);
            // defaultToolBarMgr.add(removeRowAction);
        }
    }

    public abstract String getSqlText();

    public abstract DatabaseManagerDialog getDialog();

    public TableViewerWithMultipage getTableViewer() {
        return null;
    }

    // /**
    // * Getter for cancelAction.
    // *
    // * @return the cancelAction
    // */
    // public AbstractToolAction getCancelAction() {
    // return this.cancelAction;
    // }

    /**
     * Getter for sqlRunAction.
     * 
     * @return the sqlRunAction
     */
    public AbstractToolAction getSqlRunAction() {
        return this.sqlRunAction;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.sql.IDataEditor#updateDefaultTable(java.lang.String[], java.util.List)
     */
    public void updateDefaultTable(String[] colNames, List<DataSetRow> data) {

    }
}
